package com.kanshu.ksgb.fastread.doudou.common.business.pay;

import android.app.Activity;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.JsonUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.OrderRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.WxOrderBean;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wcy on 2017\6\29 0029.
 */
public class WxPayHelper {
    private Activity mActivity;
    private boolean mPayFlg;
    private LoadingDialog mLoadingDialog;
    public WxPayHelper(Activity activity) {
        mActivity = activity;
        mLoadingDialog = new LoadingDialog(mActivity, "正在加载中");
    }
    public Activity getActivity() {
        return mActivity;
    }

    /**
     * 支付动作
     * @param params
     */
    public void payAction(OrderRequestParams params) {
        if (mPayFlg) {
            return;
        }
        mPayFlg = true;
        mLoadingDialog.show();
        RetrofitHelper helper = RetrofitHelper.getInstance();
        Map<String, String> map = JsonUtils.jsonStrToMap(params.toJsonStr());
        helper.createService(PayService.class).getWxOrder(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<WxOrderBean>() {
            @Override
            public void onResponse(BaseResult<WxOrderBean> wxOrderBeanBaseResult, WxOrderBean wxOrderBean, Disposable disposable) {
                IWXAPI api = WXAPIFactory.createWXAPI(Xutils.getContext(), wxOrderBean.appId);
                if (!api.isWXAppInstalled()) {
                    mPayFlg = false;
                    mLoadingDialog.dismiss();
                    ToastUtil.showMessage(Xutils.getContext(), "请安装微信客户端");
                    return;
                }
                api.registerApp(wxOrderBean.appId);
                PayReq payReq = new PayReq();
                payReq.appId = wxOrderBean.appId;
                payReq.partnerId = wxOrderBean.partnerId;
                payReq.prepayId = wxOrderBean.prepayId;
                payReq.packageValue = wxOrderBean.packageValue;
                payReq.nonceStr = wxOrderBean.nonceStr;
                payReq.timeStamp = wxOrderBean.timeStamp + "";
                payReq.sign = wxOrderBean.sign;
                api.sendReq(payReq);
                mPayFlg = false;
                mLoadingDialog.dismiss();
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                mPayFlg = false;
                mLoadingDialog.dismiss();
                ToastUtil.showMessage(mActivity.getResources().getString(R.string.error_back));
            }
        });
    }

}
